/************************************************************************
This file is part of lpc2xxx package for the simpleRTJ virtual machine.

This file is covered by the GNU GPL with the following exception:
  As a special exception, the copyright holders of this library give you permission
  to link this library with independent modules to produce an executable, regardless
  of the license terms of these independent modules, and to copy and distribute the
  resulting executable under terms of your choice, provided that you also meet, for
  each linked independent module, the terms and conditions of the license of that
  module. An independent module is a module which is not derived from or based on
  this library. If you modify this library, you may extend this exception to your
  version of the library, but you are not obligated to do so. If you do not wish
  to do so, delete this exception statement from your version.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Copyright (c) 2000-2006 RTJ Computing Pty. Ltd. All rights reserved.
***********************************************************************/
package lpc2xxx.edu;

import lpc2xxx.hw.*;

/**
 * Activates the GPIO pins P0.16-20, P0.14 for input and reads the state of joystick switch 
 * and SW2 pushbutton.
 */
public class Switch extends Thread
{
    /** Instance of PORT0 GPIO pins */
    private GPIO io = new GPIO(GPIO.PORT0);
    /** Last state of joystick and SW2 switch */
    private int state;

    /**
     * Creates and initializes the instance of Switch object.
     */
    public Switch()
    {
        GPIO.config(0, 0xCFFFFFFF, 0);
        GPIO.config(1, 0xFFFFFC00, 0);
        io.dirIn(GPIO.P14 | GPIO.P16 | GPIO.P17 | GPIO.P18 | GPIO.P19 | GPIO.P20);
        setDaemon(true);
    }

    /**
     * Checks if any of the joystick keys or SW2 is activated.
     * @return true if any of the keys is down.
     */
    public boolean keyDown()
    {
        int mask = GPIO.P14 | GPIO.P16 | GPIO.P17 | GPIO.P18 | GPIO.P19 | GPIO.P20;
        return (state & mask) != mask;
    }

    /**
     * Checks the state of <i>center</i> switch on the Joystick.
     * @return true if <i>center</i> switch is pushed in.
     */
    public boolean center()
    {
        return (state & GPIO.P16) == 0;
    }

    /**
     * Checks the state of <i>left</i> switch on the Joystick.
     * @return true if <i>left</i> switch is pushed in.
     */
    public boolean left()
    {
        return (state & GPIO.P19) == 0;
    }

    /**
     * Checks the state of <i>up</i> switch on the Joystick.
     * @return true if <i>up</i> switch is pushed in.
     */
    public boolean up()
    {
        return (state & GPIO.P17) == 0;
    }
    
    /**
     * Checks the state of <i>right</i> switch on the Joystick.
     * @return true if <i>right</i> switch is pushed in.
     */
    public boolean right()
    {
        return (state & GPIO.P18) == 0;
    }

    /**
     * Checks the state of <i>down</i> switch on the Joystick.
     * @return true if <i>down</i> switch is pushed in.
     */
    public boolean down()
    {
        return (state & GPIO.P20) == 0;
    }

    /**
     * Checks the state of <i>SW2</i> switch.
     * @return true if <i>SW2</i> switch is pushed in.
     */
    public boolean sw2()
    {
        return (state & GPIO.P14) == 0;
    }

    /**
     * <i>run</i> method of <i>Switch</i> class that continuously checks the state of 
     * Joystick and SW2 switch. If a monitored button is depressed (or released) a simple 
     * time delayed debouncing is performed to eliminate intermitent pulses that occur 
     * during button's state change.
     */
    public void run()
    {
        state = io.read();
        while (true)
        {
            int v = io.read();
            if (v != state) 
            {
                // perform simple switch debouncing
                try 
                {
                    sleep(50);
                }
                catch (InterruptedException e) {}
                if (io.read() == v) 
                    state = v;
            }
            yield();
        }
    }
}

